package com.tfjybj.itoo.exam.provider.controller;

import com.dmsdbj.itoo.tool.business.ItooResult;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.exam.entity.QuestionClassifyEntity;
import com.tfjybj.itoo.exam.model.QuestionClassifyModel;
import com.tfjybj.itoo.exam.provider.service.QuestionClassifyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * QuestionClassifyController
 * questionClassify表
 *
 * @author 李青松
 * @version 2.0.0
 * @since 2.0.0 2018-11-01 08:55:53
 */
@Api(tags = {"questionClassify表接口"})
@RequestMapping(value = "/questionClassify")
@RestController
@Slf4j
public class QuestionClassifyController {

    @Resource
    private QuestionClassifyService questionClassifyService;

    //region 模板生成：基本增删改

    /**
     * 添加
     *
     * @param model QuestionClassifyModel
     * @return 添加的结果
     * @author 李青松
     * @since 2.0.0 2018-11-01 08:55:53
     */
    @ApiOperation(value = "添加")
    @PostMapping(value = {"/create"})
    public ItooResult create(@RequestBody QuestionClassifyModel model) {
        if (StringUtils.isEmpty(model.getName())) {
            return ItooResult.build(ItooResult.FAIL, "classifyName为空");
        }
        QuestionClassifyEntity questionClassifyEntity = new QuestionClassifyEntity();
        BeanUtils.copyProperties(model, questionClassifyEntity);
        questionClassifyService.save(questionClassifyEntity);
        return ItooResult.build(ItooResult.SUCCESS, "添加成功");
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return ItooResult 是否删除成功
     * @author 李青松
     * @since 2.0.0 2018-11-01 08:55:53
     */
    @ApiOperation(value = "根据id删除（单个）")
    @DeleteMapping(value = {"/delete/{id}"})
    public ItooResult delete(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        questionClassifyService.removeById(id);
        return ItooResult.build(ItooResult.SUCCESS, "删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return ItooResult 批量删除是否成功结果
     * @author 李青松
     * @since 2.0.0 2018-11-01 08:55:53
     */
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping(value = {"/deleteByIds"})
    @ApiImplicitParam(name = "ids", value = "ids", dataType = "List<String>", required = true)
    public ItooResult deleteByIds(@RequestBody List<String> ids) {
        questionClassifyService.removeByIds(ids);
        return ItooResult.build(ItooResult.SUCCESS, "批量删除成功");
    }

    /**
     * 修改
     *
     * @param model QuestionClassifyModel
     * @return 修改后的结果
     * @author 李青松
     * @since 2.0.0 2018-11-01 08:55:53
     */
    @ApiOperation(value = "根据id修改questionClassify")
    @PutMapping(value = {"/modify"})
    public ItooResult modify(@RequestBody QuestionClassifyModel model) {
        if (StringUtils.isEmpty(model.getName())) {
            return ItooResult.build(ItooResult.FAIL, "classifyName为空");
        }
        QuestionClassifyEntity questionClassifyEntity = new QuestionClassifyEntity();
        BeanUtils.copyProperties(model, questionClassifyEntity);
        questionClassifyService.updateById(questionClassifyEntity);
        return ItooResult.build(ItooResult.SUCCESS, "修改成功");
    }

    /**
     * @param id 主键id
     * @return 根据id查找QuestionClassify     *
     * @author 李青松
     * @since 2.0.0 2018-11-01 08:55:53
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = {"/findById/{id}"})
    public ItooResult findById(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        QuestionClassifyEntity questionClassifyEntity = questionClassifyService.getById(id);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", questionClassifyEntity);
    }

    /**
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @return 分页查询所有QuestionClassify     *
     * @author 李青松
     * @since 2.0.0 2018-11-01 08:55:53
     */
    @ApiOperation(value = "分页查询所有QuestionClassify")
    @GetMapping(value = {"/queryPageAll/{pageNo}/{pageSize}"})
    public ItooResult queryPageAll(@ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                   @ApiParam(name = "pageSize", value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<QuestionClassifyEntity> questionClassifys = questionClassifyService.queryPageAll(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", questionClassifys);
    }

    /**
     * 根据父试题分类id模糊查询questionClassify
     *
     * @param pId      父试题分类id
     * @param pageNo   页码
     * @param pageSize 页数
     * @return 模糊查询的questionClassify
     * @author 李青松
     * @since 2.0.0 2018-11-01 08:55:53
     */
    @ApiOperation(value = "根据父试题分类id模糊查询questionClassify", notes = "分页根据父试题分类id模糊查询questionClassify")
    @GetMapping(value = "queryQuestionClassifyByLikePId/{pageNo}/{pageSize}")
    public ItooResult queryQuestionClassifyByLikePId(@RequestParam(required = false, defaultValue = "") String pId,
                                                     @ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                                     @ApiParam(name = "pageSize", value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<QuestionClassifyEntity> questionClassifyList = questionClassifyService.queryByLikePId(pId, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", questionClassifyList);
    }
    //endregion

    /* **********************************以下为非模板生成的内容********************************* */


    /**
     * 根据操作人查询questionClassify
     *
     * @param operator 操作人operator
     * @return 模糊查询的questionClassify
     * @author 郭凯
     * @since 2.0.0 2018-11-01 08:55:53
     */

    @ApiOperation(value = "根据操作人查询questionClassify")
    @GetMapping(value = "/queryByOperator/{operator}")
    public ItooResult queryClassify(@ApiParam(value = "操作人", required = true) @PathVariable String operator) {
        if (operator != null || !"".equals(operator)) {
            return ItooResult.build(ItooResult.SUCCESS, "查询成功", questionClassifyService.queryClassify(operator));
        } else {
            return ItooResult.build(ItooResult.FAIL, "操作人不可为空");
        }
    }

    /**
     * 根据操作人查询questionClassify
     *
     * @param operator 操作人operator
     * @return 模糊查询的questionClassify
     * @author 郭凯
     * @since 2.0.0 2018-11-01 08:55:53
     */
    @ApiOperation(value = "根据题型查询questionClassify")
    @GetMapping(value = "/queryByQuestionTypeId/{questionTypeId}")
    public ItooResult queryByQuestionTypeId(@RequestParam(required = false, defaultValue = "") String operator,
                                            @PathVariable String questionTypeId) {
        List<QuestionClassifyEntity> questionClassifyEntityList = questionClassifyService.queryQuestionClassify(operator, questionTypeId);
        if (questionClassifyEntityList.size() > 0) {
            return ItooResult.build(ItooResult.SUCCESS, "查询成功", questionClassifyEntityList);
        } else {
            return ItooResult.build(ItooResult.FAIL, "暂无数据", null);
        }

    }


    /**
     * 添加questionClassifyModel的树形结构
     *
     * @param questionClassifyModelList 树形结构对象
     * @return 添加是否成功信息
     * @author 郭凯
     * @since 2.0.0 2018-11-30 09:01:09
     */
    @ApiOperation("添加questionClassifyModel的树形结构")
    @PostMapping(value = {"/createTreeModel"})
    public ItooResult createTreeModel(@RequestBody List<QuestionClassifyModel> questionClassifyModelList) {
        if (questionClassifyService.createTreeModel(questionClassifyModelList)) {
            return ItooResult.build(ItooResult.SUCCESS, "添加成功");
        } else {
            return ItooResult.build(ItooResult.FAIL, "添加失败");
        }
    }

    /**
     * 查询questionClassifyModel的树形结构
     *
     * @param courseId 课程id
     * @return itooResult
     * @author 郭凯
     * @since 2.0.0 2018-12-5 11:10:50
     */
    @ApiOperation("查询questionClassifyModel的树形结构")
    @GetMapping(value = {"/queryTreeModel/{courseId}/{operator}"})
    public ItooResult queryTreeModel(@ApiParam(value = "课程id", required = true) @PathVariable String courseId,
                                     @ApiParam(value = "操作人id", required = true) @PathVariable String operator) {
        List<QuestionClassifyEntity> questionClassifyEntityList = questionClassifyService.queryTreeModel(courseId,operator);
        if (questionClassifyEntityList.size() > 0) {
            return ItooResult.build(ItooResult.SUCCESS, "查询成功", questionClassifyEntityList);
        } else {
            return ItooResult.build(ItooResult.FAIL, "查询失败", null);
        }
    }


    /**
     * 根据操作人,课程,题型查询questionClassify
     *
     * @param courseid       课程id courseid
     * @param questiontypeid 题型id questiontypeid
     * @return 根据操作人, 课程, 题型查询questionClassify
     * @author 杨海云
     * @since 2.0.0 2018-11-01 08:55:53
     */
    @ApiOperation(value = "根据操作人,课程,题型查询questionClassify")
    @GetMapping(value = "/queryQuestionClassfiyByQuestiontypeid/{questiontypeid}/{courseid}")
    public ItooResult queryQuestionClassfiyByQuestiontypeid(
            @ApiParam(value = "课程id", required = true) @PathVariable String courseid,
            @ApiParam(value = "题型id", required = true) @PathVariable String questiontypeid) {
        List<QuestionClassifyModel> questionClassifyModelList = questionClassifyService.queryQuestionClassfiyByQuestiontypeid(courseid, questiontypeid);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", questionClassifyModelList);
    }


    /**
     * 根据课程id查询分类
     *
     * @param courseId
     * @return tooResult 根据课程id查询分类
     * @author 贾建清
     * @since 2.0.0 2018-12-17 14:55:49
     */
    @ApiOperation(value = "根据课程id查询分类")
    @GetMapping(value = {"/queryQuestionClass/{courseId}"})
    public ItooResult queryQuestionClass(@ApiParam(value = "课程id", required = true) @PathVariable String courseId

    ) {
        List<QuestionClassifyModel> questionClassifyModelList = questionClassifyService.queryQuestionClass(courseId);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", questionClassifyModelList);

    }


    /**
     * 根据操作人/课程/题型查询分类
     *
     * @param courseId
     * @param questionTypeId
     * @return ItooResult 根据操作人/课程/题型查询分类
     * @author 贾建清
     * @since 2.0.0 2018-12-17 19:48:48
     */
    @ApiOperation(value = "根据操作人/课程/题型查询分类")
    @GetMapping(value = "/queryQuestionClassByType/{courseId}/{questionTypeId}")
    public ItooResult queryQuestionClassByType(
            @ApiParam(value = "课程id", required = true) @PathVariable String courseId,
            @ApiParam(value = "题型id", required = true) @PathVariable String questionTypeId) {
        List<QuestionClassifyModel> questionClassifyModelList = questionClassifyService.queryQuestionClassByType(courseId, questionTypeId);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", questionClassifyModelList);
    }

    /**
     * 添加questionClassifyModel的树形结构
     *
     * @param questionClassifyModelList 树形结构对象
     * @return 添加是否成功信息
     * @author 郭凯
     * @since 2.0.0 2018-11-30 09:01:09
     */

    /**
     *
     * @param questionClassifyModel
     * @return
     * @author 杜雨
     * @since 2.0.0 2019年6月19日10:57:57
     */
    @ApiOperation("添加questionClassifyNode的树节点")
    @PostMapping(value = {"/createTreeNode"})
    public ItooResult createTreeNode(@RequestBody QuestionClassifyModel questionClassifyModel) {
        if(questionClassifyModel==null){
            log.info("添加questionClassifyNode的树节点 参数questionClassifyModel为空！");
           return ItooResult.build(ItooResult.FAIL, "分类为空！");
        }
        QuestionClassifyEntity classifyEntity = questionClassifyService.createTreeNode(questionClassifyModel);
        if (classifyEntity!=null && !StringUtils.isEmpty(classifyEntity.getId())) {
            return ItooResult.build(ItooResult.SUCCESS,"添加成功", classifyEntity.getId());
        } else {
            return ItooResult.build(ItooResult.FAIL, "添加失败");
        }
    }

}
